package com.leve.control.service;

import com.leve.control.domain.Live;
import com.leve.control.util.RedisUtil;
import com.leve.control.util.qiniu.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class LiveService {

    @Autowired
    private RedisUtil redisUtil;

    String accessKey = "t-Vxpo2aj37pc5gc6xDW3r_dECdHvyuktmAHYqEy";
    String secretKey = "HIaJ-rVhOf97tNzM9B1gf1KXvFC6n41G6JhxCjQU";


    String hubName = "lw-controller";

    //初始化client
    Client cli = new Client(accessKey, secretKey);
    //初始化Hub
    Hub hub = cli.newHub("lw-controller");

    public Live startLive(String deviceId) {
        String streamKeyPrefix = "javasdkexample" + new Date().getTime();
        Config.APIHost = "pili.qiniuapi.com";

        //获得不存在的流
        String keyA = streamKeyPrefix + "A";

        try{
            hub.get(keyA);
        }catch (PiliException e){
            if (e.isNotFound()) {
                System.out.printf("Stream %s doesn't exist\n", keyA);
            }else {
                System.out.println(keyA + " should not exist");
                e.printStackTrace();
                return null;
            }
        }
        System.out.printf("keyA=%s 不存在\n",keyA);

        //创建流
        try {
            hub.create(keyA);
        }catch (PiliException e){
            e.printStackTrace();
            return null;
        }
        System.out.printf("keyA=%s 创建\n", keyA);

        // RTMP推流地址
        String pubUrl = cli.RTMPPublishURL("pili-publish.www.levepower.net", hubName, keyA, 3600);
        System.out.printf("keyA=%s RTMP推流地址=%s\n", keyA, pubUrl);

        //RTMP直播地址
        String liveUrl = cli.RTMPPlayURL("pili-live-rtmp.www.levepower.net", hubName, keyA);
        System.out.printf("keyA=%s RTMP直播地址=%s\n", keyA, liveUrl);

        Live live = new Live();
        live.setPubUrl(pubUrl);
        live.setLiveUrl(liveUrl);
        live.setKey(keyA);

        redisUtil.set(deviceId,live,1L, TimeUnit.DAYS);
        return live;

    }

    private static String printArrary(Object[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (Object a : arr) {
            sb.append(a.toString() + " ");
        }
        sb.append("]");
        return sb.toString();
    }


    public Live getLiveInfo(String deviceId) {

        Live live = (Live)redisUtil.get(deviceId);
        if(null == live){
            return null;
        }
        String keyA = live.getKey();

        //获得流
        Stream streamA;
        try{
            streamA = hub.get(keyA);
        }catch (PiliException e){
            e.printStackTrace();
            return null;
        }
        System.out.printf("keyA=%s 查询: %s\n", keyA, streamA.toJson());

        //查询直播状态
        try {
            Stream.LiveStatus status = streamA.liveStatus();
            live.setStatus(status.toJson());
            System.out.printf("keyA=%s 直播状态: status=%s\n", keyA, status.toJson());
        } catch (PiliException e) {
            if (!e.isNotInLive()) {
                e.printStackTrace();
            } else {
                System.out.printf("keyA=%s 不在直播\n", keyA);
            }
        }
        return live;
    }
}
